package be.kuleuven.cs.mop.domain.model.impl;


import be.kuleuven.cs.mop.domain.exceptions.TaskManagerException;
import be.kuleuven.cs.mop.domain.model.UserType;



public class UserTypeImpl extends WorldElement implements UserType  {
	
	private String name;
	
	
	public UserTypeImpl(final World world, final String name) throws TaskManagerException {
		super(world);
		setName(name);
		
		getWorld().add(this);
	}
	
	@Override
	public boolean equals(Object o){
		return (o != null) && o instanceof UserTypeImpl && ((UserTypeImpl)o).getName().equals(this.getName());
	}
	
	
	
	@Override
	public String getName() { return name; }
	
	private void setName(final String name) throws TaskManagerException {
		if(!isValidName(name))
			throw new TaskManagerException(TaskManagerException.USER_TYPE_INVALID_NAME);
		this.name = name;
	}
	
	private boolean isValidName(String name) {
		return (name != null && !name.isEmpty() && !nameAlreadyInUse(name));
	}
	
	private boolean nameAlreadyInUse(String name) {
		for(UserTypeImpl uType : getWorld().getUserTypes()) {
			if(uType.getName().equals(name))
				return true;
		}
		return false;
	}

	@Override
	public boolean isAdmin(){
		return false;
	}
	
	@Override
	public String toString() {
		return getName();
	}
	
}